using System;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowDataTypes
{
	//*********************************************************************
	//
	// DataType Class
	//
	// The XmlDataType class represents a set of enumeration values witch
	// can be associated to na application or scipt or subflow like its result
	//
	//*********************************************************************

	public class XMLDataType : bOS.XML.Data.XMLObject
	{
		public const string MAIN_NODE				= "DataType";

		private const string KEY_ATTRIBUTE			= "Key";
		private const string TYPE_ATTRIBUTE			= "Type";
		private const string DESCRIPTION_NODE		= "Description";

		//System DataType
		public const string PARTICIPANTS_DATATYPE		= "Partecipants";
		public const string WORKFLOWPROCESSES_DATATYPE	= "WorkflowProcesses";
		public const string SUBFLOWEXECUTION_DATATYPE	= "SubflowExecution";
		public const string NUMBER_DATATYPE				= "Number";



		/** FIELDS **/

		private string sKey;
		private string sType;
		private string sDescription;

		private XMLEnumerationValues enumerationValues;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLDataType()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLDataType(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** PROPERTIES **/

		public XMLEnumerationValues XMLEnumerationValues 
		{
			get { return enumerationValues; }
			set { enumerationValues = value; }
		}


		public string Key 
		{
			get { return sKey; }
			set { sKey = value; }
		}

		public string Type 
		{
			get { return sType; }
			set { sType = value; }
		}

		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		/** INSTANCE METHODS **/

		/** Inizializing method **/
		override public void Init()
		{
			sKey= string.Empty;
			sType= string.Empty;
			sDescription= string.Empty;

			enumerationValues = new XMLEnumerationValues(); 
		}

		/** Parsering method for filling the value **/
		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<nodeList.Attributes.Count; i++)
			{
				node= nodeList.Attributes.Item(i);

				switch (node.Name)
				{
					case XMLDataType.KEY_ATTRIBUTE:
						sKey = node.InnerText;
						break;

					case XMLDataType.TYPE_ATTRIBUTE:
						sType = node.InnerText;
						break;
				}
			}

			/** find the sub tags witch contains values **/
			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLDataType.DESCRIPTION_NODE:
						sDescription = node.InnerText;
						break;

					case XMLEnumerationValues.MAIN_NODE:
						enumerationValues.Parse(node);
						break;
				}
			}
		}

		/**Method to retive a node from object **/
		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLDataType.MAIN_NODE);
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute ( XMLDataType.KEY_ATTRIBUTE, sKey) );
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute ( XMLDataType.TYPE_ATTRIBUTE, sType) );
			
			xmlNode.AddNode ( new bOS.XML.XMLNode (XMLDataType.DESCRIPTION_NODE, sDescription) );
			xmlNode.AddNode ( this.enumerationValues.GetXMLNode() );

			return xmlNode;
		}
	}
}